package com.qf.bank.test;/*
 * @author     : XXX
 * @date       : 2022/5/4 15:22
 * @description: some description
 */

import com.qf.bank.entity.Account;
import com.qf.bank.service.AccountService;
import com.qf.bank.service.impl.AccountServiceImpl;

import java.math.BigDecimal;
import java.util.Scanner;

public class ATMDemo {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("`````1登录```2存款```3取款````4转账```0退出````````````");
        boolean flag=true;
        Account loginAccount=null;
        //调用业务逻辑层
        AccountService accountService = new AccountServiceImpl();
        do {
            System.out.println("请选择");
            int choice = input.nextInt();
            switch (choice){
                case 1:
                    try {
                        System.out.println("请输入卡号：");
                        String cardNo=input.next();
                        System.out.println("请输入密码：");
                        String password=input.next();
                        loginAccount=accountService.login(cardNo,password);
                        if (loginAccount != null){
                            System.out.println("登录成功");
                        }else{
                            System.out.println("卡号或密码输入错误");
                        }
                    } catch (Exception e) {
                        System.out.println("登录失败"+e.getMessage());
                    }
                    break;
                case 2:
                    if (loginAccount!=null) {//防止空指针异常
                        try {
                            System.out.println("请输入存款金额：");
                            double money = input.nextDouble();
                            accountService.save(loginAccount.getCardNo(),new BigDecimal(money));
                            System.out.println("存款成功");
                        } catch (Exception e) {
                            System.out.println("存款失败"+e.getMessage());
                        }
                    }else {
                        System.out.println("请登录再存款");
                    }
                    break;
                case 3:
                    if (loginAccount!=null) {//防止空指针异常
                        try {
                            System.out.println("请输入取款金额：");
                            double money = input.nextDouble();
                            accountService.take(loginAccount.getCardNo(),new BigDecimal(money));
                            System.out.println("取款成功");
                        } catch (Exception e) {
                            System.out.println("取款失败"+e.getMessage());
                        }
                    }else {
                        System.out.println("请登录再取款");
                    }
                    break;
                case 4:
                    if (loginAccount!=null) {
                        try {
                            System.out.println("请输入对方卡号:");
                            String toCardNo= input.next();
                            System.out.println("请输入转账金额:");
                            double money = input.nextDouble();
                            boolean b = accountService.transMoney(loginAccount.getCardNo(), toCardNo, new BigDecimal(money));
                            if (!b){
                                System.out.println("转账成功");
                            }else {
                                System.out.println("转账失败");
                            }
                        } catch (Exception e) {
                            System.out.println("转账失败:"+e.getMessage());
                        }
                    }else {
                        System.out.println("请先登录再操作");
                    }
                    break;
                case 0:
                    flag=false;
                    break;
                default:
                    System.out.println("输入有误请重新输入");
                    break;
            }
        }while(flag);
        System.out.println("欢迎下次光临");
    }
}
